package it.polito.connectedcar.network;

import it.polito.connectedcar.network.VehicleMessage.VehicleMessageCode;
import de.uniluebeck.itm.tcpip.Storage;

public class NeighborhoodMessage extends Message {

	public boolean isSessionKeyChange() {
		return sessionKeyChange;
	}

	public void setSessionKeyChange(boolean sessionKeyChange) {
		this.sessionKeyChange = sessionKeyChange;
	}


	private boolean sessionKeyChange;
	private NeighborhoodMessageCode neighborhoodMessageCode;
	
	public NeighborhoodMessage() {
		super(EntityType.NEIGHBORHOOD);
	}
	public NeighborhoodMessage(byte[] data)
	{
		super(EntityType.NEIGHBORHOOD,data);
	}
	@Override
	protected void convertToData(Storage st) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	protected void convertFromData(Storage st) {
		this.setNeighborhoodMessageCode(NeighborhoodMessageCode.fromCode(st.readByte()));
		
		int expectedMaxAttrCount = 2;
		int counter = 0;
		while(st.validPos() && counter<expectedMaxAttrCount)
		{
			extractAttributes(st);
			counter++;
		}
		
	}
	
	

	public enum NeighborhoodMessageCode
	{
		ADD_NEIGHBOR(0),
		REMOVE_NEIGHBOR(1),
		SESSION_KEY_CHANGE(2);
		
		private final int code;
		private NeighborhoodMessageCode(int code) {
			this.code = code;
		}
		
		public int getCode()
		{
			return this.code;
		}
		public static NeighborhoodMessageCode fromCode(int neighborhoodMessageCode)
		{
			for(NeighborhoodMessageCode nmc : NeighborhoodMessageCode.values())
			{
				if(nmc.getCode()==neighborhoodMessageCode)
					return nmc;
			}
			throw new IllegalArgumentException("no VehicleMessageCode exists for the specified code");
		}
	}
	
	public NeighborhoodMessageCode getNeighborhoodMessageCode() {
		return neighborhoodMessageCode;
	}

	public void setNeighborhoodMessageCode(NeighborhoodMessageCode neighborhoodMessageCode) {
		this.neighborhoodMessageCode = neighborhoodMessageCode;
	}

}
